package com.milhouz.wormdroid.gl.factory;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import android.util.Log;

import com.milhouz.wormdroid.gl.model.anim.Animation;

public class BotAnimationFactory {

	public final static int WORMS_BOT_TYPE = 0;
	public final static int MARIO_BOT_TYPE = 1;

	private final static Animation[] wormsAnims = new Animation[4];
	private final static Animation[] marioAnims = new Animation[4];

	private static BotAnimationFactory instance;

	public static BotAnimationFactory getInstance() {
		if (instance == null) {
			instance = new BotAnimationFactory();
		}
		return instance;
	}

	public Animation[] getMarioAnims() {
		return marioAnims;
	}

	public Animation[] getWormsAnims() {
		return wormsAnims;
	}

	public void loadAnimationsFromFile(int botType, InputStream isFile) {
		if (botType == WORMS_BOT_TYPE ) {
			loadFromFile(isFile, wormsAnims);
		} else if (botType == MARIO_BOT_TYPE) {
			loadFromFile(isFile, marioAnims);
		}
	}

	private void loadFromFile(InputStream isFile, Animation[] anims) {
		final char[] buffer = new char[0x10000];
		StringBuilder out = new StringBuilder();
		Reader in;
		try {
			in = new InputStreamReader(isFile, "UTF-8");
			int read;
			do {
				read = in.read(buffer, 0, buffer.length);
				if (read > 0) {
					out.append(buffer, 0, read);
				}
			} while (read >= 0);
		} catch (IOException e) {
			Log.e("wormdroid", "Impossible to read the animbot file.");
		}
		Log.d("wormdroid", " File : \n" + out.toString());
		loadFromString(out.toString().split("\\\n"), anims);
	}

	/**
	 * Load the animations texture coordonates in the animation array. 
	 * Line template :
	 * name; delay; [x,y,w,h] [x',y',w',h'] ...
	 * <ul>
	 * <li>name : Animation name</li>
	 * <li>delay : time to wait between each image</li>
	 * <li>[x,y,w,h] : coordonates in the texture atlas</li>
	 * </ul>
	 * @param animStr File content (one string per line)
	 * @param anims AnimationArray
	 */
	private void loadFromString(String[] animStr, Animation[] anims) {
		// Optimize or change all of that !
		Animation curAnim = null;
		for (int i = 0; i < animStr.length; i++) {
			String[] cols = animStr[i].split(";");
			Log.d("wormdroid", " line : " + i);
			if (cols.length == 2) {
				curAnim = new Animation(cols[0]);
				String[] coordonatesList = cols[1].split("\\|");
				Log.d("wormdroid", " anim : " + cols[0]);
				for (int j = 0; j < coordonatesList.length; j++) {
					String[] coordonates = coordonatesList[j].split(",");
					Log.d("wormdroid", " coordonates : " + coordonatesList[j]);
					if (coordonates.length == 5) {
						curAnim.add(Integer.valueOf(coordonates[0])
								, Integer.valueOf(coordonates[1])
								, Integer.valueOf(coordonates[2])
								, Integer.valueOf(coordonates[3])
								, Integer.valueOf(coordonates[4]));
					}
				}
			}
			if (i<anims.length)
				anims[i] = curAnim;
		}
	}

}
